package com.csyf.manager.service.impl;

import com.csyf.manager.dao.SysAliAccountDao;
import com.csyf.manager.dao.SysWithdrawalRecordDao;
import com.csyf.manager.entity.SysAliAccount;
import com.csyf.manager.entity.SysWithdrawalRecord;
import com.csyf.manager.exception.ApiValidateException;
import com.csyf.manager.service.SysWithdrawalRecordService;
import com.csyf.manager.utils.NumberHelpler;
import com.csyf.manager.utils.page.JqGridVo;
import com.csyf.manager.utils.page.PageUtils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author: casper
 * @Description: 提现记录ServiceImpl
 * @Date: Created in 2018/10/27 17:37
 */
@Slf4j
@Service
public class SysWithdrawalServiceImpl implements SysWithdrawalRecordService {

    @Autowired
    private SysAliAccountDao sysAliAccountDao;

    @Autowired
    private SysWithdrawalRecordDao sysWithdrawalRecordDao;

    @Autowired
    private RedisService redisService;


    @Override
    public JqGridVo querydata(SysWithdrawalRecord vo, Integer rows, Integer page) {
        PageHelper.startPage(page, rows);
        PageHelper.orderBy("create_time DESC");

        Example example = new Example(SysWithdrawalRecord.class);
        Example.Criteria criteria = example.createCriteria();
        PageUtils.andEqualTo(criteria, "appId", vo.getAppId());
        PageUtils.andBetweenDateToDate(criteria, "createTime", vo.getWithdrawalCreateTimeStr(), vo.getWithdrawalEndTimeStr());
        List<SysWithdrawalRecord> select = sysWithdrawalRecordDao.selectByExample(example);

        return PageUtils.pasrJqGrid(select);
    }


    @Override
    public BigDecimal queryTotalCase(String srcmch_id, String date) {
        return sysWithdrawalRecordDao.queryTotalCase(srcmch_id,date);
    }

    /**
     * 提现
     * @param accountId
     * @param withdrawMoney
     */
    @Override
    public void withdrawalMoney(Long accountId, BigDecimal withdrawMoney) throws ApiValidateException {

        SysAliAccount aliAccount = sysAliAccountDao.selectByPrimaryKey(accountId);

        //校验提现金额
        valiWithdrawMoney(aliAccount,withdrawMoney);

        //增加提现记录
        SysWithdrawalRecord vo = new SysWithdrawalRecord();
        vo.setAppId(aliAccount.getAppid());
        vo.setChannelNo(aliAccount.getChannelNo());
        vo.setCreateTime(new Date());
        vo.setMoney(withdrawMoney);
        vo.setRemark("");
        sysWithdrawalRecordDao.insert(vo);

        //更新账户余额
//        BigDecimal balance = aliAccount.getAccountMoney().subtract(withdrawMoney);
//        aliAccount.setAccountMoney(balance);
//        sysAliAccountDao.updateByPrimaryKey(aliAccount);
    }

    public void valiWithdrawMoney(SysAliAccount aliAccount, BigDecimal withdrawMoney) throws ApiValidateException {
        boolean number = NumberHelpler.isNumber(withdrawMoney.toString());
        if (!number) {
            throw new IllegalArgumentException("不是有效金额");
        }

//        if (withdrawMoney.compareTo(aliAccount.getAccountMoney()) > 0) {
//            throw new ApiValidateException("提现金额超限！");
//        }

        BigDecimal minmoney = BigDecimal.ONE;
        if (minmoney.compareTo(withdrawMoney) > 0) {
            throw new ApiValidateException("提现金额最低为1元");
        }





    }



}
